# coding=utf-8
from pymongo import MongoClient

# 建立和mongodb的链接
client = MongoClient(host="localhost", port=27017)

# 选择要操作的数据库和集合
collection = client["test"]["t1"]

# 插入数据操作
# ret = collection.insert_one({"name": "haha", "age": 20})
# print(ret)

# 插入多条数据 insert_many(item), item为多条数据

# 查找数据
ret2 = collection.find_one({"name": "haha"})
# 查找多条数据，find(),结果是一个cursor游标对象，遍历读取

# 更新
ret3 = collection.update_one({"name": "haha"}, {"$set": {"name": "heihei"}})
print(ret3)

# 更新全部
ret4 = collection.update({"name": "haha"}, {"$set": {"name": "hehe"}})

# 删除
# 删除一条
collection.delete_one({"name": "heihei"})

# 删除全部
collection.delete_many({"age": 20})

